#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 10, 9, 8, 7, 3);

//Set the button pin for button
int switchOnOff = 2;
int buttonPin1 = 5;
int buttonPin2 = 6;
int buttonPin3 = 4;
int LEDGreen = 12;
int LEDRed = 13;

// Variable
int OnOff = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int count = 0;
int systemWork = 0;
int x = 1;
int xxx = 0;
int RandomSet = 0;

//Time
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
unsigned long time5 = 0;
unsigned long timeDummyStart = 0;
unsigned long timeDummyStop = 0;
unsigned long timeRand = 0;

//Sending value
int valInP = 0;
char valInA = 0;

void setup() {
  Serial.begin(9600);                    // Start serial communication at 9600 bps
  lcd.begin(16,2);
  pinMode(switchOnOff, INPUT);
  pinMode(buttonPin1, INPUT);             // Set pin 0 as an input
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(LEDGreen, OUTPUT);
  pinMode(LEDRed, OUTPUT);
}

//This code is arduino code for finding responding time.
//It contains 5 times in 1 set.
//After finished it all, it will show all responding time result.
//When the LED turn Green, it means that the system is ready for the next time.
//The RED LED shows the button is already push and you need to wait for the next time.

void loop() {
  
  OnOff = digitalRead(switchOnOff);
  if(OnOff == HIGH) {
    systemWork = 1;
    //Clear LCD screen
    lcd.setCursor(0, 0);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
  }
  
  if(count < 5 && systemWork == 1) {
    //tell the program status
    digitalWrite(LEDRed, HIGH);
    digitalWrite(LEDGreen, HIGH);
    delay(1000);
    lcd.setCursor(0, 0);
    lcd.print("   Sys Ready!   "); 
    digitalWrite(LEDRed, LOW);
    digitalWrite(LEDGreen, HIGH);
    timeRand = random(1000,4000);
    delay(timeRand);
    RandomSet = random(0,10);
    Serial.print(RandomSet);
    //-----------------------3 case RGB
    //Send the ascii number to be character case for processing (82->R, 71->G, 66->B)
    if(RandomSet < 3) {
      xxx = 1;
      Serial.write(82);
      timeDummyStart = millis();
    }
    else if (RandomSet >= 3 && RandomSet < 7) {
      xxx = 2;
      Serial.write(71);
      timeDummyStart = millis();
    }
    else if (RandomSet >= 7 && RandomSet <10) {
      xxx = 3;
      Serial.write(66);
      timeDummyStart = millis();
    }
    
    //infinity loop 
    //waiting for responsing
    while (x > 0) {
      //Push R button
      if(digitalRead(buttonPin1) == HIGH) {
         //if the current color is red, then do this.
         if(xxx == 1) {
            timeDummyStop = millis();
            Serial.write('x');
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         //Other color
         else {
            //--------------ERROR (push wrong button)
            timeDummyStop = timeDummyStart;
            Serial.write('x');
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         break;
      }
      //Push G button
      else if (digitalRead(buttonPin2) == HIGH) {
         //if the current color is green, then do this.
         if(xxx == 2) {
            timeDummyStop = millis();
            Serial.write('x'); 
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         //Other color
         else {
            //--------------ERROR (push wrong button)
            timeDummyStop = timeDummyStart;
            Serial.write('x');
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         break;
      }
      //Push B button
      else if (digitalRead(buttonPin3) == HIGH) {
         //if the current color is blue, then do this.
         if(xxx == 3) {
            timeDummyStop = millis();
            Serial.write('x');
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         //Other color
         else {
            //--------------ERROR (push wrong button)
            timeDummyStop = timeDummyStart;
            Serial.write('x');
            digitalWrite(LEDGreen, LOW);
            digitalWrite(LEDRed, HIGH);
         }
         break;
      }
    }
    lcd.setCursor(0, 0);
    lcd.print("     Wait!!     "); 
//-------------------------------------------------------
    //Calculate the responding time for 5 tests in 1 Set
    if(count == 0) {
      time1 = timeDummyStop - timeDummyStart;
    }
    else if (count == 1) {
      time2 = timeDummyStop - timeDummyStart;
    }
    else if (count == 2) {
      time3 = timeDummyStop - timeDummyStart;
    }
    else if (count == 3) {
      time4 = timeDummyStop - timeDummyStart;
    }
    else if (count == 4) {
      time5 = timeDummyStop - timeDummyStart;
    }
    delay(1000);
    count++;
  }
  
  // Print all of the responding time to the screen
  else if(count == 5 && systemWork ==1) {
    //Print the responding time:
    lcd.setCursor(0, 0);
    lcd.print("Responding time:");
    
    //Print time 1
    lcd.setCursor(0, 1);
    if (time1 > 0) {
      lcd.print("Time1 = "); 
      lcd.setCursor(8, 1);
      lcd.print((float)time1/1000);
    }
    else {
      lcd.print("Time1 = -    "); 
    }
    delay(2000);
    
    //clear the lcd screen
    lcd.setCursor(0, 0);
    lcd.print("                "); 
    lcd.setCursor(0, 1);
    lcd.print("                "); 
    
    //Print time2
    lcd.setCursor(0, 0);
    if (time2 > 0) {
      lcd.print("Time2 = ");  
      lcd.setCursor(8, 0);
      lcd.print((float)time2/1000);
    }
    else {
      lcd.print("Time2 = -    "); 
    }
    delay(2000);
    
    //Print time3
    lcd.setCursor(0, 1);
    if (time3 > 0) {
      lcd.print("Time3 = ");  
      lcd.setCursor(8, 1);
      lcd.print((float)time3/1000);
    }
    else {
      lcd.print("Time3 = -    "); 
    }
    delay(2000);
    
    //Print time 4
    lcd.setCursor(0, 0);
    if (time4 > 0) {
      lcd.print("Time4 = ");  
      lcd.setCursor(8, 0);
      lcd.print((float)time4/1000);
    }
    else {
      lcd.print("Time4 = -    "); 
    }
    delay(2000);
    
    //Print time 5
    lcd.setCursor(0, 1);
    if (time5 > 0) {
      lcd.print("Time5 = ");  
      lcd.setCursor(8, 1);
      lcd.print((float)time5/1000);
    }
    else {
      lcd.print("Time5 = -    "); 
    }
    delay(2000);
    
    count = 0;
    systemWork = 0;
    digitalWrite(LEDGreen, LOW);
    digitalWrite(LEDRed, LOW);
  }
}
